package com.yisen.dasan.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yisen.dasan.dao.UserMapper;
import com.yisen.dasan.entity.FormResponse;
import com.yisen.dasan.pojo.Form;
import com.yisen.dasan.pojo.User;
import com.yisen.dasan.service.MailService;
import com.yisen.dasan.service.UserService;
import com.yisen.dasan.util.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.ExecutorService;

/**
 * @AuthorList: LiuYiSen
 * @Date: 2020/10/3 14:22
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
//    @Autowired
//    RabbitTemplate rabbitTemplate;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MailService mailService;
    @Autowired
    ExecutorService executorService;
    /**
     * 该方法用来验证登陆用户用户名和密码
     * @param username
     * @param password
     *
     * @return
     */
    @Override
    public String login(String username, String password) {
        User user = new User(0,username,password,0,"","");
        String token = "";
        user = userMapper.login(user);
        Map<String, String> map = new HashMap<>();
        if(user==null){
            return "账号或密码错误";
        }else if (user!=null && user.getState()!=0){
            map.put("id", String.valueOf(user.getId()));
            map.put("username", user.getUsername());
            token = JWTUtils.getToken(map);
        }else if(user.getState()==0){
            return "邮件激活";
        }
        return token;
    }

    @Override
    @Transactional
    public boolean register(String username, String password,String mail) throws MessagingException {
        int n = userMapper.isRegister(username);
        if(n>0)
            return false;
        // 写入数据库
        String key = UUID.randomUUID().toString().replace("-", "");
        User user = new User(0,username,password,0,key,mail);
        userMapper.register(user);
        // 添加消息队列进行发送激活邮件
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("key", key);
//        jsonObject.put("mail", mail);
        executorService.execute(()->{
            try {
                mailService.userRegister(key,mail);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        });

//        rabbitTemplate.convertAndSend("register", jsonObject);

        return true;
    }

    @Override
    public boolean affirm(String key) {
        int n = userMapper.affirm(key);
        if (n>0)
            return true;
        return false;
    }

    /**
     * 返回该用户创建的所有表单
     * @param request
     * @return
     */
    @Override
    public PageInfo<FormResponse> selForm(int pageNum,HttpServletRequest request){
        String token = request.getHeader("token");
        DecodedJWT verify = JWTUtils.verify(token);
        Integer id = Integer.parseInt(verify.getClaims().get("id").asString());
//        User user = new User();// FIXME 模拟用户
//        user.setId(100001);
//        int id = user.getId();
        PageHelper.startPage(pageNum, 9);
        List<FormResponse> forms = userMapper.selForm(id);
        System.out.println(forms instanceof Page);
        PageInfo<FormResponse> pageInfo=new PageInfo<>(forms);
        return pageInfo;
    }
    private int getCount(int formId){
        int count=userMapper.getCount(formId);
        return count;
    }
}
